package com.huixin.powermonitor.controller;


import com.huixin.powermonitor.common.Result;
import com.huixin.powermonitor.entity.Menu;
import com.huixin.powermonitor.entity.User;
import com.huixin.powermonitor.service.MenuService;
import com.huixin.powermonitor.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huixin
 * @since 2025-02-21
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单权限接口")
public class MenuController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    @ApiOperation("获取所有菜单列表")
    private List<Menu> list(){
        return menuService.list();
    }

    // 根据用户角色查找菜单权限
    @GetMapping("/role/{role}")
    @ApiOperation("根据角色ID获取对应菜单权限")
    private Result roleMenu(@PathVariable Integer role) {
        return Result.suc(menuService.lambdaQuery().like(Menu::getMenuright,role).list());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据用户ID获取其权限菜单")
    private Result userMenu(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.suc(menuService.lambdaQuery().like(Menu::getMenuright,user.getRole()).list());
    }
}